package org.dbstar.scheduler.actions;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.dbstar.scheduler.beans.ItemRelative;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;


/**
 * 视频相关性
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-7-19
 */
public class ItemRelatedAction extends ActionSupport {
	private static final RowMapper<ItemRelative> mapper = new RowMapper<ItemRelative>() {
		@Override
		public ItemRelative mapRow(ResultSet rs, int rowNum) throws SQLException {
			return new ItemRelative(rs.getInt("item1"), rs.getInt("item2"), rs.getInt("rate"));
		}
	};

	private JdbcTemplate jdbcTemplate;

	private int item;

	private List<ItemRelative> tops;
	private List<ItemRelative> tops1;
	private List<ItemRelative> topItems;
	private List<ItemRelative> topsBefore;
	private List<ItemRelative> topsAfter;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public int getItem() {
		return item;
	}

	public void setItem(int item) {
		this.item = item;
	}

	public List<ItemRelative> getTops() {
		return tops;
	}

	public List<ItemRelative> getTops1() {
		return tops1;
	}

	public List<ItemRelative> getTopItems() {
		return topItems;
	}

	public List<ItemRelative> getTopsBefore() {
		return topsBefore;
	}

	public List<ItemRelative> getTopsAfter() {
		return topsAfter;
	}

	@Override
	public String execute() throws Exception {
		this.tops = jdbcTemplate.query("select * from MatchByUser order by rate desc limit 10", mapper);
		this.tops1 = jdbcTemplate.query("select * from MatchByLastPlay order by rate desc limit 10", mapper);

		if (item > 0) {
			this.topItems = jdbcTemplate.query("select * from MatchByUser where item1=" + item + " or item2=" + item
					+ " order by rate desc limit 10", mapper);
			this.topsBefore = jdbcTemplate.query("select * from MatchByLastPlay where item2=" + item
					+ " order by rate desc limit 10", mapper);
			this.topsAfter = jdbcTemplate.query("select * from MatchByLastPlay where item1=" + item
					+ " order by rate desc limit 10", mapper);
		}
		return SUCCESS;
	}
}
